package com.chen.disruptor;

import com.alibaba.fastjson.JSON;
import com.chen.enums.MatchStrategy;
import com.chen.match.MatchServiceFactory;
import com.chen.model.Order;
import com.chen.model.OrderBooks;
import com.chen.model.OrderEvent;
import com.lmax.disruptor.EventHandler;
import lombok.Data;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @Author: chen
 * @Date: 2021/5/24 2:52 PM
 * @Version 1.0
 */

@Data
public class OrderMatchDisruptorHandler implements EventHandler<OrderEvent> {

    private static final Logger LOG = LoggerFactory.getLogger(OrderMatchDisruptorHandler.class);

    private OrderBooks orderBooks;

    private String symbol ;

    public OrderMatchDisruptorHandler(OrderBooks orderBooks) {
        this.orderBooks = orderBooks;
        this.symbol =  this.orderBooks.getSymbol() ;
    }

    @Override
    public void onEvent(OrderEvent event, long l, boolean b) throws Exception {
        Order order = (Order) event.getSource();
        LOG.info("====> 开始接受委托单{} <======", JSON.toJSONString(order));
        if(!order.getSymbol().equals(symbol)){ // 我们接收到了一个不属于我们处理的数据,我们不处理
            return;
        }
        //这里进行撮合
        MatchServiceFactory.getMatchService(MatchStrategy.LIMIT_PRICE).match(orderBooks,(Order) event.getSource());

        try {
        } catch (Exception e) {
            LOG.error(String.format("处理委托单：%s 发生了错误", JSON.toJSON(order)), e);
        } finally {

        }
    }

}
